package com.wsl.Jwt;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: wsl
 * @Date: 2022/2/15 14:14
 */
public class JwtUtil {
    //过期时间
    private static final long EXPIRE_TIME = 10 * 60 * 1000;

    //token私钥
    //private static final String TOKEN_SECRET = "fa546asd2rt31uio456pljgs21sdf89wzx1";
    private static final String TOKEN_SECRET = "wsl20000522blogbackstagechinawu123.0";

    private static final String ISSUER = "WSL_BLOG_API";


    /*
     * 生成签名，15min后过期
     *
     * @param username 用户名
     * @return 加密的token
     * */

    public static String sign(String username, String userId) {


        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            //私钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("typ", "JWT");
            header.put("alg", "HS256");
            //附带username，userID信息，生成签名
            return JWT.create()
                    .withHeader(header)
                    .withIssuer(ISSUER)
                    .withIssuedAt(new Date())
                    //.withJWTId() 编号
                    .withClaim("loginName", username)
                    .withClaim("userId", userId)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 校验token 是否正确
     */
    public static DecodedJWT verify(String token) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier build = JWT.require(algorithm).withIssuer(ISSUER).build();
            DecodedJWT verify = build.verify(token);
            return verify;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取token中的信息无需secret解密也可以获得
     */
    public static String getUserName(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("loginName").asString();
        } catch (JWTDecodeException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     *  token 过期时间
     */
    public static Date getTokenTime(String token)throws Exception{
        DecodedJWT jwt = JWT.decode(token);
        Date expiresAt = jwt.getExpiresAt();
        return expiresAt;
    }

    public static String getUserID(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userId").asString();
        } catch (JWTDecodeException e) {
            e.printStackTrace();
            return null;
        }
    }
}